<?php
namespace app\admin\controller;
use app\admin\model\Image;
use app\admin\model\Chart as ChartModel;

//轮播图管理类
class Chart extends Base
{
    /**
     * 轮播图列表
     */
    public function lst()
    {
        $list = ChartModel::order('ct_create_time DESC')->paginate(10);

        $info['code']    = '200';
        $info['data']    = $list;
        echo displayJson($info);die;
    }

    /**
     * 添加轮播图
     */
    public function addChart()
    {
        $data['ct_status'] = input('status');

        $stream  = input('image');
        $tmpName = uploadBase64($stream); //将流文件保存为临时文件
        $suffix  = getSuffix($tmpName);  //获取后缀名

        if ($suffix !== false) {
            $realName = 'chart_img_'.time().'_'.make_Verification_Code(6).'.'.$suffix;
            $ret      = rename($tmpName,$realName);

            if ($ret) {
                //压缩图片
                $source =  $realName;//原图片名称
                $dst_img = './image/chart/'.$realName;//压缩后图片的名称
                @(new Image($source))->compressImg($dst_img);

                @unlink($realName);

                //插入数据
                $new_dst_img      = substr($dst_img, 1);
                $data['ct_image'] = 'http://www.taoerhuo.top/bishe/public'.$new_dst_img;

                $data['ct_create_time'] = time();

                $res = db('chart',[],false)->insert($data);

                if ($res) {
                    $info['code']    = '200';
                    $info['message'] = '添加成功';
                    echo displayJson($info);die;
                } else {
                    $info['code']    = '400';
                    $info['message'] = '添加失败';
                    echo displayJson($info);die;
                }
            } else {
                $info['code']    = '400';
                $info['message'] = '图片获取失败';
                echo displayJson($info);die;
            }
        }
    }

    /**
     * 更新轮播图状态
     */
    public function changeStatus()
    {
        $status = input('status'); //当前状态值
        $id     = input('id');     //轮播图id

        if (!$status || !$id) {
            $info['code']    = '400';
            $info['message'] = '参数丢失';
            echo displayJson($info);die;
        }

        if ($status == 1) {
            //禁用
            $set['ct_status'] = 2;
        } elseif ($status == 2) {
            //启用
            $set['ct_status'] = 1;
        } else {
            $info['code']    = '400';
            $info['message'] = '参数错误';
            echo displayJson($info);die;
        }

        $res = db('chart',[],false)->where('ct_id',$id)->update($set);

        if ($res) {
            $info['code']    = '200';
            $info['message'] = '更新成功';
            echo displayJson($info);die;
        } else {
            $info['code']    = '400';
            $info['message'] = '更新失败';
            echo displayJson($info);die;
        }

    }

    /**
     * 删除轮播图
     */
    public function delChart()
    {
        $id = input('id');

        if (!$id) {
            $info['code']    = '400';
            $info['message'] = '参数丢失';
            echo displayJson($info);die;
        }

        //查找
        $row = db('chart',[],false)->where('ct_id',$id)->find();

        if (!$row) {
            $info['code']    = '400';
            $info['message'] = '轮播图不存在，请刷新后重试';
            echo displayJson($info);die;
        }

        $to_arr   = explode('/',$row['ct_image']);

        $img_name = end($to_arr); //图片名字

        //删除图片
        @unlink('./image/chart/'.$img_name);

        $del = db('chart',[],false)->where('ct_id',$id)->delete();

        if ($del) {
            $info['code']    = '200';
            $info['message'] = '删除成功';
            echo displayJson($info);die;
        } else {
            $info['code']    = '400';
            $info['message'] = '删除失败';
            echo displayJson($info);die;
        }
    }


}